package com.jy.api.controller.admin;

import com.jy.api.controller.admin.params.RegionAddParams;
import com.jy.api.controller.admin.params.RegionEditParams;
import com.jy.api.dao.NumDao;
import com.jy.api.dao.RegionDao;
import com.jy.api.domain.Num;
import com.jy.api.domain.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "v1/admin/region", produces = "application/json;charset=utf-8")
public class RegionController {

    @Autowired
    private RegionDao regionDao;
    @Autowired
    private NumDao numDao;


    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/edit")
    public ResponseEntity edit(@RequestBody RegionEditParams params) {
        Region region;
        if (params.getId() > 0) {
            region = regionDao.findOne(params.getId());
            if (region == null) {
                return ResponseEntity.badRequest().body("你要编辑的区域不存在");
            }
        } else {
            region = new Region();
        }
        region.setStoreId(params.getStoreId());
        region.setName(params.getName());
        region.setQueue(params.getQueue());
        regionDao.save(region);
        return ResponseEntity.ok().body("操作成功");
    }

    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity list(@RequestBody RegionEditParams params) {
        List<Region> region = regionDao.findByStoreIdOrderByQueueDesc(params.getStoreId());
        return ResponseEntity.ok(region);
    }


    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/detail")
    public ResponseEntity detail(@RequestBody RegionEditParams params) {
        Region region = regionDao.findOne(params.getId());
        return ResponseEntity.ok(region);
    }


    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/delete")
    public ResponseEntity delete(@RequestBody RegionEditParams params) {
        Region region = regionDao.findOne(params.getId());
        regionDao.delete(region);
        return ResponseEntity.ok("区域删除成功");
    }

    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("batch/add/num")
    public ResponseEntity batchAddNum(@RequestBody RegionAddParams params) {
        Region region = regionDao.findOne(params.getRegionId());
        if (region == null) {
            return ResponseEntity.badRequest().body("你要操作的区域不存在");
        }
        Num numQueue = null;
        if (region.getNums().size() > 0) {
            numQueue = region.getNums().get(region.getNums().size() - 1);
        }
        String beginIp = params.getBeginIp();
        int queue = 9999;
        if (numQueue != null) {
            queue = numQueue.getQueue();
        }
        String ip = beginIp.split("\\.")[0] + "." + beginIp.split("\\.")[1] + "." + beginIp.split("\\.")[2] + ".";
        int s = Integer.parseInt(params.getBeginIp().split("\\.")[3]);
        if (params.getCount() == 1) {
            Num num = new Num();
            num.setStoreId(region.getStoreId());
            if (queue == 9999) {
                num.setQueue(queue);
            } else {
                num.setQueue(queue - 1);
            }
            if (s < 10) {
                num.setName("0" + s);
            } else {
                num.setName(s + "");
            }
            num.setIpAddress(beginIp);
        }
        for (int i = 0; i < params.getCount(); i++) {
            Num num = new Num();
            num.setStoreId(region.getStoreId());
            num.setQueue(queue - 1);
            if (i == 0) {
                if (s < 10) {
                    num.setName("0" + s);
                } else {
                    num.setName(s + "");
                }
                num.setIpAddress(beginIp);
            } else {
                if (s + i < 10) {
                    num.setName("0" + (s + i));
                } else {
                    num.setName((s + i) + "");
                }
                num.setIpAddress(ip + (s + i));
            }
            queue -= 1;
            region.getNums().add(num);
        }
        regionDao.save(region);
        return ResponseEntity.ok(region);
    }

    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/add/num")
    public ResponseEntity addNum(@RequestBody RegionAddParams params) {
        Region region = regionDao.findOne(params.getRegionId());
        if (region == null) {
            return ResponseEntity.badRequest().body("你要操作的区域不存在");
        }
        region.getNums().add(new Num(params));
        regionDao.save(region);
        return ResponseEntity.ok(region);
    }

    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/edit/num")
    public ResponseEntity editNum(@RequestBody RegionAddParams params) {
        Num num = numDao.findOne(params.getId());
        if (num == null) {
            return ResponseEntity.badRequest().body("你要操作的桌位不存在");
        }
        num.setIpAddress(params.getIpAddress());
        num.setMacNo(params.getMacNo());
        num.setName(params.getName());
        num.setQueue(params.getQueue());
        num.setRgb(params.getRgb());
        numDao.save(num);
        return ResponseEntity.ok(num);
    }


    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/delete/num")
    public ResponseEntity deleteNum(@RequestBody RegionAddParams params) {
        Num num = numDao.findOne(params.getId());
        if (num == null) {
            return ResponseEntity.badRequest().body("你要操作的桌位不存在");
        }
        numDao.delete(num);
        return ResponseEntity.ok().body("操作成功");
    }

    /**
     * 获取所有店铺时段
     *
     * @param
     * @return
     */
    @PostMapping("/detail/num")
    public ResponseEntity detailNum(@RequestBody RegionAddParams params) {
        Num num = numDao.findOne(params.getId());
        if (num == null) {
            return ResponseEntity.badRequest().body("你要操作的桌位不存在");
        }
        return ResponseEntity.ok(num);
    }


}
